package com.example.nophase.common;

import android.content.Context;

import com.example.nophase.entity.SysUserVO;
import com.example.nophase.utils.HttpUtils;
import com.example.nophase.utils.SPUtils;

import com.example.nophase.utils.V;
import com.google.gson.Gson;

public class LoginManager {

    /**
     * 判断是否已经登录过
     * @return
     */
    public static boolean isLogined(Context context){
        return !V.isEmpty(getToken(context));
    }

    /**
     * 从登录信息中获取token
     * @return
     */
    public static String getToken(Context context){
        return String.valueOf(SPUtils.get(context,"token",""));
    }

    /**
     * 设置token
     * @param context
     * @param token
     */
    public static void setToken(Context context, String token) {
        SPUtils.put(context,"token",token);
        HttpUtils.getInstance().setAuthToken(token);
    }

    /**
     * 设置本地用户信息
     * @param context
     * @param sysUserVO
     */
    public static void setUserInfo(Context context, SysUserVO sysUserVO) {
        SPUtils.put(context,"userInfo",sysUserVO);
    }

    public static SysUserVO getUserInfo(Context context){
        Gson gson = new Gson();
                String json = (String) SPUtils.get(context,"userInfo","");
                SysUserVO sysUserVO = gson.fromJson(json,SysUserVO.class);

        return sysUserVO;

    }

    public static void clearToken(Context context) {
        SPUtils.put(context,"token","");
        HttpUtils.getInstance().setAuthToken(null);
    }





}
